package cn.itcast.order.web;

import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private OrderService orderService;

//   @Autowired
//   private RestTemplate restTemplate;

    @Autowired
    UserClient userClient;

    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId, @RequestHeader(value = "name",required = false) String name) {
        log.warn("请求头name的值: {}",name);
        // 根据id查询订单并返回
        Order order = orderService.queryOrderById(orderId);
        // 远程查询user信息
//        String url = "http://userservice/user/"+order.getUserId();
//        User user = restTemplate.getForObject(url, User.class);
        User user = userClient.queryById(order.getUserId());
        // 封装返回信息
        order.setUser(user);
        return order;
    }
}
